package com.bjpowernode.boss;

import com.bjpowernode.boss.dao.mapper.DicPayTypeMapper;
import com.bjpowernode.boss.dao.mapper.DicPayWayMapper;
import com.bjpowernode.boss.dao.result.DicEntity;
import com.bjpowernode.boss.dao.result.DicPayTypeGroup;
import jakarta.annotation.Resource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Arrays.stream;

/**
 * CommandLineRunner：Springboot中的接口，就一个run方法
 * 执行run()的时间：在Spring容器初始化完成后，在业务方法执行之前
 * run()适合做什么：
 * 		项目初始化操作，
 * 		读取数据库
 * 		读取配置文件
 * 		生成默认数据
 * 		做一些项目的初始化工作
 */
@MapperScan("com.bjpowernode.boss.dao.mapper")
@SpringBootApplication
public class PowerBossApplication implements CommandLineRunner {

	// 注入Spring容器对象
	@Resource
	private ApplicationContext applicationContext ;

	public static void main(String[] args) {
		SpringApplication.run(PowerBossApplication.class, args);
	}

	// 支付方式字典List
	public static List<DicEntity> PAYWAY_DIC_LIST = new ArrayList<>() ;
	// 支付方式字典的Map(payWayCode, payWayName)
	public static Map<String, String> PAYWAY_NAME_MAP = new HashMap<>() ;

	// 支付类型Map<wayCode, List<DicEntity>>
	public static Map<String, List<DicEntity>> PAYTYPE_DIC_MAP = new HashMap<>() ;
	// 支付类型的名称的map
	public static Map<String, String> PAYTYPE_NAME_MAP = new HashMap<>() ;

	@Override
	public void run(String... args) throws Exception {
		// 读取字典表的数据，dic_pay_way，缓存到内存中
		DicPayWayMapper dicPayWayMapper = applicationContext.getBean(DicPayWayMapper.class) ;
		PAYWAY_DIC_LIST = dicPayWayMapper.selectPayWayDicList() ;
		// 将List转为Map
		PAYWAY_NAME_MAP = PAYWAY_DIC_LIST.stream().collect(Collectors.toMap(DicEntity::getCode, DicEntity::getLabel));

		// 支付类型Map数据
		DicPayTypeMapper dicPayTypeMapper = applicationContext.getBean(DicPayTypeMapper.class) ;
		List<DicPayTypeGroup> groupList = dicPayTypeMapper.selectPayTypeGroupContent() ;
		// Map<string, list<dicEntity>>
		// ALIPAY F2F_PAY#条码支付;HUA_BEI_FEN_QI_PAY#花呗分期
		groupList.forEach(item -> {
			List<DicEntity> list = new ArrayList<>() ;
			String payWayCode = item.getPayWayCode() ;
			String content = item.getContent() ;

			String typeCode = "" ;
			String typeName = "" ;
			for (String cet : content.split(";")) {
				typeCode = cet.split("#")[0] ;
				typeName = cet.split("#")[1] ;
				list.add(new DicEntity(typeName, typeCode)) ;

				PAYTYPE_NAME_MAP.put(payWayCode + ":" + typeCode, typeName) ;
			}

			PAYTYPE_DIC_MAP.put(payWayCode, list) ;
		});

		System.out.println("PAYTYPE_DIC_MAP = " + PAYTYPE_DIC_MAP);
	}
}
